package com.ktjy.controller;

import com.ktjy.entity.User;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class UserController {
    Logger logger= LoggerFactory.getLogger(UserController.class);

//    @RequestMapping("/list")
//    public String list(HttpServletRequest request){
//        String name=request.getParameter("name");
//        String age=request.getParameter("age");
//        logger.info("list方法进来了,请求参数name={},age={}",name,age);
//        return null;
//    }

//    @RequestMapping("/list")
//    public String list(Model model){
//
//        List<User> list=new ArrayList<>();
//        list.add(new User(1,"张三",19));
//        list.add(new User(2,"李四",20));
//        list.add(new User(3,"王五",21));
//        //        request.setAttribute("list",list);
//        model.addAttribute("list",list);
//
//        return "list";
//    }
@RequestMapping("/list")
public ModelAndView list(){

    List<User> list=new ArrayList<>();
    list.add(new User(1,"张三",19));
    list.add(new User(2,"李四",20));
    list.add(new User(3,"王五",21));
    //        request.setAttribute("list",list);
    ModelAndView mv=new ModelAndView();
    mv.addObject("list",list);
    mv.setViewName("list");

    return mv;
}

    @RequestMapping("/view")
    public String view(@RequestParam("id") Integer id){
        logger.info("view方法进来了,请求参数id={}",id);
        return null;
    }

    @RequestMapping("/add")
    public String add(){
        return "add";
    }

    @RequestMapping("/save")
    public String save(User user){
        logger.info("save方法进来了,请求参数id={}，name={},age={}",
                user.getId(),user.getName(),user.getAge());
        return null;
    }
}
